﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Windows.Storage;
using System.Runtime.Serialization; 
using System.ComponentModel;
using System.Xml.Serialization;
using System.Text.RegularExpressions;

namespace Genespy
{
    [DataContract]
    public class MediaObject:INotifyPropertyChanged
    {
        
        private string _title;
        [XmlIgnore()]
        private Uri _mediaStorageUri;
        private DateTime _date; 

        public string title
        {
            get { return _title; }
            set
            {
                if (_title != value)
                {
                    _title = value;
                }
            }
        }

        public DateTime date
        {
            get { return _date; }
            set
            {
                if (_date != value)
                {
                    _date = value;
                }
            }
        }

        [XmlIgnore()]
        public Uri mediaStorageUri
        {
            get { return _mediaStorageUri; }
            set
            {
                if (_mediaStorageUri != value)
                {
                    _mediaStorageUri = value;
                }
            }
        }

        [XmlElement("URI")]
        public string _URI
        {
            get { return _mediaStorageUri.ToString(); }
            set { _mediaStorageUri = new Uri(value); }
        }

        public MediaObject(StorageFile storage, DateTime dateTime) {
            Uri uri = new Uri(storage.Path);

            var pattern = "\\S*LocalState";
            var replacement = "ms-appdata:///local";
            Regex rx = new Regex(pattern);
            string formattedUri = uri.AbsolutePath;
            formattedUri = rx.Replace(formattedUri, replacement);

            mediaStorageUri = new Uri(formattedUri);
            date = dateTime;
        }

        public event PropertyChangedEventHandler PropertyChanged;

        public void OnPropertyChanged(String propertyName)
        {
            if (PropertyChanged != null)
                PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
        }

        public MediaObject() { }
    }
}
